package com.example.springboot.service.impl;


import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.entity.Book;
import com.example.springboot.entity.RBook;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.mapper.BorrowMapper;
import com.example.springboot.mapper.RBookMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.IRBookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Service
@Slf4j
public class RBookService implements IRBookService {

    @Resource
    RBookMapper rBookMapper;

    @Autowired
    BookMapper bookMapper;
    @Autowired
    BorrowMapper borrowMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<RBook> list() {
        return rBookMapper.list();
    }

    @Override
    public PageInfo<RBook> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        // 自关联查询
        List<RBook> rBooks = rBookMapper.listByCondition(baseRequest);

        return new PageInfo<>(rBooks);
    }

    @Override
    public void save(RBook obj){
        //改状态
        obj.setStatus("已归还");

        rBookMapper.updateStatus("已归还",obj.getId());    //id是前端传回来的行数据id
        borrowMapper.updateStatus("已归还",obj.getId());

        obj.setRealDate(LocalDate.now());
        rBookMapper.save(obj);
        //图书增加
        bookMapper.updateNumByNo(obj.getBookNo());

        //返还用户积分
        Book book = bookMapper.getByNo(obj.getBookNo());
        if (book != null) {
            long until = 0;
            if (obj.getRealDate().isBefore(obj.getReturnDate())){
                until = obj.getRealDate().until(obj.getReturnDate(), ChronoUnit.DAYS);

            }else if(obj.getRealDate().isAfter(obj.getReturnDate())){   //逾期归还要扣分
                until = -obj.getReturnDate().until(obj.getRealDate(), ChronoUnit.DAYS);
            }
            int score = (int) until * book.getScore(); //获取当前待返还的积分
            //给用户加积分
            User user = userMapper.getByUserName(obj.getUserNo());
            int account = user.getAccount()+score;
            user.setAccount(account);
            if(account<0){
                user.setStatus(false);  //锁定账号
            }
            userMapper.updateById(user);
        }
    }

    @Override
    public RBook getById(Integer id) {
        return rBookMapper.getById(id);
    }

    @Override
    public void update(RBook obj) {
        obj.setUpdateTime(LocalDate.now());
        rBookMapper.updateById(obj);
    }

    @Override
    public void deleteById(Integer id) {
        rBookMapper.deleteById(id);
    }


}
